import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker
from matplotlib.backends.backend_pdf import PdfPages
# 确保日期列是 datetime 类型
# data_day['date'] = pd.to_datetime(data_day['date'])
# data_night['date'] = pd.to_datetime(data_night['date'])
#
# data = pd.merge_asof( data_day.sort_values('date'),data_night.sort_values('date'), on='date', by=['symbol', 'code','kprice'], direction='backward')
# data.to_excel("D:\pycharm project\py 文件\dasta.xlsx")


# 过滤数据：ins_iv > 1 和 des_iv <0.5  ,近月 和 远月的日内升波未降波

# 定义一个绘图函数，用于绘制符合特定条件的数据
'''各symbol 中同时满足ins_iv 大于0.01 ，des_iv < 0.05  的情况下，ins_iv的幅度分布'''
# 计算每个 symbol 的 ins_iv 范围和对应的箱数
# def calculate_bin_ranges(df, symbol, bin_width=0.002):
#     specific_data = df[df['symbol'] == symbol]
#     min_value = specific_data['ins_iv'].min()
#     max_value = specific_data['ins_iv'].max()
#
#     # 确保 bin_width 是正数
#     if bin_width <= 0:
#         bin_width = 0.002  # 设置一个默认值
#
#     # 确保 min_value 和 max_value 之间的差距足够大
#     if max_value - min_value < bin_width:
#         max_value = min_value + bin_width
#
#     # 生成区间
#     bins = np.arange(min_value, max_value + bin_width, bin_width)
#     return bins

# 绘图函数
def plot_condition_ins (df, title,sym:str = 'ins_iv'):
    n_rows = len(symbols)
    fig, axes = plt.subplots(n_rows, 1, figsize=(10, 2 * n_rows))
    for idx, symbol in enumerate(symbols):
        # bins = calculate_bin_ranges(df, symbol)
        sns.histplot(df[df['symbol'] == symbol][sym], bins=30, ax=axes[idx], color="blue", alpha=0.5)
        # axes[idx].xaxis.set_major_locator(ticker.MultipleLocator(0.01))
        axes[idx].set_title(f'{symbol} - {title}', loc='left')
        axes[idx].set_xlabel(f'{sym}')
        axes[idx].set_ylabel('Frequency')

    # plt.title(f'Distribution of {sym} for each symbol',loc='center',fontsize=20)
    # plt.title(f'Realized vs Implied Volatility for {sym}', fontsize=20)

    # plt.subplots_adjust(hspace=0.2, top=0.5, bottom=0.15)
    plt.subplots_adjust(top = 0.88)
    fig.suptitle(f'{title}', fontsize=11,y = 1.08)
    plt.tight_layout(pad =3)
    plt.show()
# 计算每个 symbol 的 ins_iv 范围和对应的箱数




# 过滤数据
# def filter_data(data_day, data_night, criteria):
#     filtered_data = {
#         'day_30': data_day[criteria(data_day) & (data_day['MIT'] <= 30)],
#         'day': data_day[criteria(data_day) & (data_day['MIT'] > 30)],
#         'day_org': data_day[criteria(data_day)],
#         'night_30': data_night[criteria(data_night) & (data_night['MIT'] <= 30)],
#         'night': data_night[criteria(data_night) & (data_night['MIT'] > 30)],
#         'night_org': data_night[criteria(data_night)]
#     }
#     return filtered_data
def process_and_plot_data(data, title_suffix, criteria_list, sym:str = "ins_iv"):
    for criteria in criteria_list:
        filtered_data = {
            'endday_45': data[criteria(data) & (data['lastdays'] <= 45)],
            'endday_over_45': data[criteria(data) & (data['lastdays'] > 45)],
            'all': data[criteria(data)]
        }

        for key, fd in filtered_data.items():
            title = f'{title_suffix}, {key.replace("_", ", ")}'
            plot_condition_ins(fd, title, sym)
            # fig = plot_condition_ins(fd, title, sym)
            # if fig:  # 如果图表被创建，则保存到 PDF
            #     pdf.savefig(fig)
            #     plt.close(fig)
# def filter_data(data, criteria):
#     filtered_data = {
#         'day_30': data[criteria(data) & (data['MIT'] <= 30)],
#         'day': data[criteria(data) & (data['MIT'] > 30)],
#         'day_org': data[criteria(data)],
#
#     }
#     return filtered_data
# # def filtered_data(df,criteria):
# # filtered_data_day_30 = data_day[criteria(data_day) & (data_day['MIT'] <= 30)]
# # filtered_data_day = data_day[criteria(data_day) & (data_day['MIT'] > 30)]
# # filtered_data_day_org = data_day[criteria(data_day)]
# # filtered_data_night_30 = data_night[criteria(data_night) & (data_night['MIT'] <= 30)]
# # filtered_data_night = data_night[criteria(data_night) & (data_night['MIT'] > 30)]
# # filtered_data_night_org = data_night[criteria(data_night)]
# # 绘制图表
# # 使用函数过滤数据
#
# def process_and_plot_data(data, title_suffix, criteria):
#     filtered_data_sets = {
#         'data_30': data[criteria & (data['MIT'] <= 30)],
#         'data_over_30': data[criteria & (data['MIT'] > 30)],
#         'data_org': data[criteria]
#     }
#
#     for key, filtered_data in filtered_data_sets.items():
#         title = f'{title_suffix}, {key.replace("_", ", ")}'
#         plot_condition_ins(filtered_data, title)

# 定义你的条件函数
# def my_criteria(df):
#     return (df['ins_iv'] > 0.01) & (df['des_iv'] < 0.05)

# 加载你的数据
'''获取数据'''
# data_day = pd.read_excel("E:\py 文件\py 文件\high_low_day.xlsx")
data_day = pd.read_excel("D:\pycharm project\py 文件\high_low_day.xlsx")
# data_night = pd.read_excel("E:\py 文件\py 文件\high_low_night.xlsx")
data_night = pd.read_excel("D:\pycharm project\py 文件\high_low_night.xlsx")
data = pd.concat([data_day, data_night], ignore_index=True)
# data.to_excel("E:\py 文件\py 文件\data123.xlsx")

# 定义 symbols 和过滤条件
symbols = ["TA", 'MA', 'RM', 'SR', 'CF', 'PK', 'OI']
criteria = lambda df:  (df['des_iv'] < 0.005)
# (0.05> df['ins_iv'])
criteria_des = lambda df:  (df['des_iv'] >= 0.005)
# criteria_list = [criteria, criteria_des]


 # 使用 PDF 保存所有图表
# with PdfPages('output_plots.pdf') as pdf:
for data_set, title_prefix in [(data_day, 'Day'), (data_night, 'Night'), (data, 'Overall')]:
    new_title_prefix = f"{title_prefix} (des_iv < 0.005)"
    process_and_plot_data(data_set, new_title_prefix, [criteria], "ins_iv")

    new_title_prefix = f"{title_prefix} (des_iv >= 0.005)"
    process_and_plot_data(data_set, new_title_prefix, [criteria_des], "des_iv")

# for data_set, title_prefix in [(data_day, 'Day'), (data_night, 'Night'), (data, 'Overall')]:
#     process_and_plot_data(data_set, title_prefix, criteria, "ins_iv")
#
# for data_set, title_prefix in [(data_day, 'Day'), (data_night, 'Night'), (data, 'Overall')]:
#     process_and_plot_data(data_set, title_prefix, criteria, "des_iv")
# # 应用函数
# process_and_plot_data(data_day, 'Day',criteria)
# process_and_plot_data(data_night, 'Night', criteria)
# process_and_plot_data(data, 'Overall', criteria)
#
# process_and_plot_data(data_day, 'Day',criteria_des)
# process_and_plot_data(data_night, 'Night', criteria_des)
# process_and_plot_data(data, 'Overall', criteria_des)
#
#
# filtered_datasets = filter_data(data_day, criteria)
# filtered_datasets_night = filter_data(data_night, criteria)
# filtered_datasets_overall = filter_data(data, criteria)
#
# process_and_plot_data(data_day, 'Day',criteria_des)
# process_and_plot_data(data_night, 'Night', criteria_des)
# process_and_plot_data(data, 'Overall', criteria_des)

# 现在您可以访问过滤后的数据集
# filtered_data_day_30 = filtered_datasets['day_30']
# filtered_data_day = filtered_datasets['day']
# filtered_data_day_org  = filtered_datasets['day_org']
#
# filtered_data_night_30= filtered_datasets_night ['night_30']
# filtered_data_night= filtered_datasets_night ['night']
# filtered_data_night_org = filtered_datasets_night ['night_org']
#
# plot_condition_ins (filtered_data_day_30, 'day,des<0.05,MIT <= 30')
# plot_condition_ins (filtered_data_day, 'day,des<0.05,MIT > 30')
# plot_condition_ins (filtered_data_day_org , 'day,Overall')
#
# plot_condition_ins (filtered_data_night_30, 'night,des<0.05,MIT <= 30')
# plot_condition_ins (filtered_data_night , 'night,des<0.05,MIT > 30')
# plot_condition_ins (filtered_data_night_org , 'night,Overall')

# filtered_data_day_30= data_day[(data_day['ins_iv'] < 0.05) & (data_day['des_iv'] < 0.5) & (data_day['MIT'] <= 30)]
# filtered_data_day= data_day[(data_day['ins_iv'] < 0.05) & (data_day['des_iv'] < 0.5) & (data_day['MIT'] > 30)]
# # filtered_data_day= data_day[data_day['des_iv'] < 0.5]
# filtered_data_night_30= data_night[(data_night['ins_iv'] < 0.05) & (data_night['des_iv'] < 0.5) & (data_night['MIT'] <= 30)]
# filtered_data_night= data_day[(data_night['ins_iv'] < 0.05) & (data_night['des_iv'] < 0.5) & (data_night['MIT'] > 30)]
# # 绘制条形图
# # plt.figure(figsize=(10, 6))
# # sns.barplot(x='ins_iv', y='symbol', data=filtered_data_day)
# # plt.title('Sum of ins_iv for Each Symbol (ins_iv > 0, des_iv = 0)')
# # plt.xlabel('Sum of ins_iv')
# # plt.ylabel('Symbol')
# # plt.show()
#
# symbols = ["TA",'MA','RM','SR','CF','PK','OI']
# # Updating specific_codes based on user's requirement
# # specific_codes = ["TA403", "MA403", "RM403", "PK403", "SR403", "CF403"]
#
# '''各symbol 中同时满足ins_iv 大于0.01 ，des_iv < 0.05  的情况下，ins_iv的幅度分布'''
# # Number of rows needed for subplots (one row for each code)
# n_rows = len(symbols)
#
# # Creating a large figure to accommodate all subplots
# fig, axes = plt.subplots(n_rows, 2, figsize=(4, 2 * n_rows))
#
# for idx, symbol in enumerate(symbols):
#     # Filter data for each specific code
#     specific_data = filtered_data_day[filtered_data_day['symbol'] == symbol]
#     specific_data_30 = filtered_data_day_30[filtered_data_day_30['symbol'] == symbol]
#     # filtered_data_day_30
#     # filtered_data_day
#     # Distribution of ins_iv for specific code
#     sns.histplot(specific_data['ins_iv'],  bins =50, ax=axes[idx],color = "blue", label= '远月')
#     # sns.histplot(specific_data_30 ['ins_iv'], bins=50, ax=axes[idx],color = "red",label= '近月')
#
#     # 设置 x 轴的刻度间隔为 0.002
#     axes[idx].xaxis.set_major_locator(ticker.MultipleLocator(0.002))
#
#     axes[idx].set_title(f'Distribution of ins_iv for {symbol}',loc='left')
#     axes[idx].set_xlabel('ins_iv')
#     axes[idx].set_ylabel('Frequency')
#     # axes[idx].legend()
#
#     # Distribution of des_iv for specific code
#     # sns.histplot(specific_data['des_iv'], bins=20, ax=axes[idx, 1])
#     # axes[idx, 1].set_title(f'Distribution of des_iv for {symbol}')
#     # axes[idx, 1].set_xlabel('des_iv')
#     # axes[idx, 1].set_ylabel('Frequency')
#     # plt.title('Distribution of ins_iv > 0 & des_iv=0 ', fontsize=5)    #
# plt.tight_layout()
# plt.show()
#
# '''各symbol 中同时满足ins_iv 大于0 ，des_iv = 0  的情况下，ins_iv的幅度众数'''
# # 计算每个 symbol 的 ins_iv 众数
# # symbol_ins_iv_mode = filtered_data_day.groupby('symbol')['ins_iv'].agg(lambda x: pd.Series.mode(x).iloc[0])
# #
# # # 重置索引以便于绘图
# # symbol_ins_iv_mode = symbol_ins_iv_mode.reset_index()
# #
# # # 绘制条形图
# # plt.figure(figsize=(10, 6))
# # barplot = sns.barplot(x='ins_iv', y='symbol', data=symbol_ins_iv_mode)
# #
# # # 添加标注
# # for p in barplot.patches:
# #     width = p.get_width()  # 获取条形的宽度
# #     plt.text(p.get_width(), p.get_y() + p.get_height() / 2,'{:1.4f}'.format(width), ha='left', va='center')  # 在条形的外侧添加标注
# #
# # plt.xlabel('Mode of ins_iv')
# # plt.ylabel('Symbol')
# # plt.title('Mode of ins_iv for Each Symbol (ins_iv > 0, des_iv = 0)')
# # plt.show()
#
# '''统计ins_iv > = 0.01, des_iv > = 0.005的分布情况     '''
# # 过滤数据：ins_iv > 0 和 des_iv = 0
# filtered_day= data_day[data_day['des_iv'] >= 0.005]
# # Creating a large figure to accommodate all subplots
# fig, axes = plt.subplots(n_rows, 2, figsize=(5, 2 * n_rows))
#
# for idx, symbol in enumerate(symbols):
#     # Filter data for each specific code
#     specific_day = filtered_day[filtered_day['symbol'] == symbol]
#
#     # Distribution of ins_iv for specific code _day
#     sns.histplot(specific_day['des_iv'], bins=20, ax=axes[idx, 0])
#     axes[idx, 0].set_title(f'Distribution of ins_iv for {symbol}',loc='left')
#     axes[idx, 0].set_xlabel('ins_iv')
#     axes[idx, 0].set_ylabel('Frequency')
#
#     # Distribution of des_iv for specific code_night
#     sns.histplot(specific_day['des_iv'], bins=20, ax=axes[idx, 1])
#     axes[idx, 1].set_title(f'Distribution of des_iv for {symbol}',loc='left')
#     axes[idx, 1].set_xlabel('des_iv')
#     axes[idx, 1].set_ylabel('Frequency')
#
# plt.tight_layout()
# plt.show()


'''   '''

'''概率'''
# 过滤数据：ins_iv > 0.01 和 des_iv > 0.007
# filtered_day_trade = data_day[(data_day['ins_iv'] > 0.01) & (data_day['des_iv'] > 0.007)]
#
# # 计算每个 symbol 满足条件的概率
# symbol_probability = filtered_day_trade.groupby('symbol').size() / data_day.groupby('symbol').size()
#
# # 重置索引以便于绘图
# symbol_probability_df = symbol_probability.reset_index()
# symbol_probability_df.columns = ['symbol', 'probability']
#
# # 绘制条形图
# plt.figure(figsize=(10, 6))
# sns.barplot(x='probability', y='symbol', data=symbol_probability_df)
# barplot1 = sns.barplot(x='ins_iv', y='symbol', data=symbol_ins_iv_mode)
#
# # 添加标注
# for p1 in barplot1.patches:
#     width1 = p1.get_width()  # 获取条形的宽度
#     plt.text(p1.get_width(), p1.get_y() + p1.get_height() / 2,'{:1.3f}'.format(width1), ha='left', va='center')  # 在条形的外侧添加标注
#
# plt.xlabel('Probability')
# plt.ylabel('Symbol')
# plt.title('Probability of Each Symbol (ins_iv > 0.01, des_iv > 0.007)')
# plt.show()
#
#
#
#




# # Updating specific_codes based on user's requirement
# specific_codes = ["TA403", "MA403", "RM403", "PK403", "SR403", "CF403"]
#
#
#
#
#
#
#
# # Creating smoothed distribution plots for ins_iv and des_iv for the specified symbols
# fig, axes = plt.subplots(len(symbols), 2, figsize=(5, 2* len(symbols)))
# title_size =8
# for idx, symbol in enumerate(symbols):
#     symbol_data_day = data_day[data_day['code'].str.startswith(symbol)]
#     symbol_data_night = data_night[data_night['code'].str.startswith(symbol)]
#
#     # Smoothed distribution plot for ins_iv
#     sns.kdeplot(symbol_data_day['ins_iv'], ax=axes[idx, 0], fill=True,color='blue', label='Day')
#     sns.kdeplot(symbol_data_night['ins_iv'], ax=axes[idx, 0], fill=True,color='red', label='Night')
#     axes[idx, 0].set_title(f'Smoothed Distribution of ins_iv for {symbol}',fontsize=title_size,loc='left')
#     axes[idx, 0].set_xlabel('ins_iv')
#     axes[idx, 0].set_ylabel('Density')
#
#     # Smoothed distribution plot for des_iv
#     sns.kdeplot(symbol_data_day['des_iv'], ax=axes[idx, 1], fill=True,color='blue', label='Day')
#     sns.kdeplot(symbol_data_night['ins_iv'], ax=axes[idx, 1], fill=True, color='red', label='Night')
#     axes[idx, 1].set_title(f'Smoothed Distribution of des_iv for {symbol}',fontsize=title_size,loc='left')
#     axes[idx, 1].set_xlabel('des_iv')
#     axes[idx, 1].set_ylabel('Density')
#     axes[idx,1].legend()
#
# plt.tight_layout()
# plt.show()
#
# '''统计近月合约的盈利情况'''
# # Calculating probabilities
#
# codes=['TA403','MA403','RM403','CF403','PK403','SR403','OI403']
# # fig, axes = plt.subplots(len(codes), 2, figsize=(5, 2* len(codes)))
# # title_size =8
# # for idx, code in enumerate(codes):
# #
# #     data_for_code_day = data_day [data_day['code']== code]
# #     data_for_code_night = data_night[data_night['code'] == code]
# #
# #     ins_iv_day_001 = (data_for_code_day['ins_iv'] > 0.01).mean()
# #     des_iv_day_0007 = (data_for_code_day['des_iv'] > 0.007).mean()
# #
# #     ins_iv_night_001 = (data_for_code_night['ins_iv'] > 0.01).mean()
# #     des_iv_night_0007 = (data_for_code_night['des_iv'] > 0.007).mean()
# #
# #     # Plot for ins_iv > 0.01
# #     axes[idx, 0].bar('Day',ins_iv_day_001, color='blue')
# #     axes[idx, 0].bar('Night',ins_iv_night_001,color='red')
# #     axes[idx,0].set_title(f'Probability of ins_iv > 0.01 for {code}')
# #     axes[idx,0].set_ylabel('Probability')
# #
# #     # Plot for des_iv > 0.007
# #     axes[idx,1].bar('Day',des_iv_day_0007,color='blue')
# #     axes[idx,1].bar('Night',des_iv_night_0007,color='red')
# #     axes[idx,1].set_title(f'Probability of des_iv > 0.007 for {code}')
# #     axes[idx,1].set_ylabel('Probability')
# #     axes[idx, 1].legend()
# #
# # plt.tight_layout()
# # plt.show()
#
#
# # Calculating probabilities for each code
# day_ins_iv_probs = []
# night_ins_iv_probs = []
# day_des_iv_probs = []
# night_day_des_iv_probs = []
#
# for code in codes:
#     data_for_code_day = data_day[data_day['code'] == code]  # Assuming using data_day for both ins_iv and des_iv
#     data_for_code_night = data_night[data_night['code'] == code]
#
#     ins_iv_day_001 = (data_for_code_day['ins_iv'] > 0.01).mean()
#     des_iv_day_0007 = (data_for_code_day['des_iv'] > 0.007).mean()
#
#     ins_iv_night_001 = (data_for_code_night['ins_iv'] > 0.01).mean()
#     des_iv_night_0007 = (data_for_code_night['des_iv'] > 0.007).mean()
#
#     day_ins_iv_probs.append(ins_iv_day_001)
#     night_ins_iv_probs.append(ins_iv_night_001)
#
#     day_des_iv_probs.append(des_iv_day_0007)
#     night_day_des_iv_probs.append(des_iv_night_0007)
#
# # Creating plots
# fig, ax = plt.subplots(2, 1, figsize=(10, 10))
#
# # Plot for ins_iv > 0.01
# ax[0].bar(codes,  day_ins_iv_probs, color='blue',label='Day')
# ax[0].bar(codes, night_ins_iv_probs, color='green',label='Night')
# ax[0].set_title('Probability of ins_iv > 0.01 for Each Contract')
# ax[0].set_xlabel('Contract Code')
# ax[0].set_ylabel('Probability')
# ax[0].legend()
# # Plot for des_iv > 0.007
# ax[1].bar(codes, day_des_iv_probs, color='blue',label='Day')
# ax[1].bar(codes, night_day_des_iv_probs, color='green',label='Night')
# ax[1].set_title('Probability of des_iv > 0.007 for Each Contract')
# ax[1].set_xlabel('Contract Code')
# ax[1].set_ylabel('Probability')
# ax[1].legend()
#
# plt.tight_layout()
# plt.show()
#
